<template>
  <div>
    <div>
      <div class="header flex">
        <el-button type="primary" @click="add()" icon="el-icon-plus">新增</el-button>
        <el-button type="danger" @click="clear()"  icon="el-icon-delete">删除</el-button>
        <!-- 弹框  新增-->
        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="类型" :label-width="formLabelWidth">
      <el-radio v-model="form.level" label="目录" @input="rad2">目录</el-radio>
      <el-radio v-model="form.level" label="菜单" @input="rad">菜单</el-radio>
    </el-form-item>
    <el-form-item label="菜单名称" :label-width="formLabelWidth">
      <el-input v-model="form.title" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item v-if="isMenu" label="上级菜单" :label-width="formLabelWidth">
      <el-select v-model="form.lastMenu" placeholder="请选择">
    <el-option
      v-for="item in options2"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="菜单路由" :label-width="formLabelWidth">
      <el-input v-model="form.path" placeholder="路由"></el-input>
    </el-form-item>
    <el-form-item label="页面" :label-width="formLabelWidth">
      <el-input v-model="form.component" placeholder="权限"></el-input>
    </el-form-item>
    <el-form-item label="权限" :label-width="formLabelWidth">
  <el-tag
 :key="tag"
 v-for="tag in form.auth"
 closable
 :disable-transitions="false"
 @close="handleClose(tag)">
 {{tag}}
</el-tag>
<el-select  class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @change="handleInputConfirm">
  <el-option
   v-for="item in options"
   :key="item.value"
   :label="item.label"
   :value="item.value">
  </el-option>
 </el-select>
<el-button v-else class="button-new-tag" size="small" @click="showInput">加入</el-button>
    </el-form-item>
    <el-form-item label="图标" :label-width="formLabelWidth" required>
      <el-popover
  placement="right"
  width="200"
  trigger="click"
  v-model="visible">
  <div>
    <i v-for="(item,i) in gridData" :key="i" :class="item.icon" style="width: 60px;font-size: 25px;text-align: center;" @click="icon(item.icon)"></i>
    <el-link type="primary" @click="fn">更多>>></el-link>

  </div>
  <el-input v-model="form.titleIcon" placeholder="菜单图标" slot="reference"></el-input>
</el-popover>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm(form)">确 定</el-button>
  </div>
</el-dialog>
<el-dialog title="菜单修改" :visible.sync="dialogFormVisible2">
  <el-form :model="form2">
    <el-form-item label="类型" :label-width="formLabelWidth">
      <el-radio v-model="form2.level" label="目录" @input="rad2">目录</el-radio>
      <el-radio v-model="form2.level" label="菜单" @input="rad">菜单</el-radio>
    </el-form-item>
    <el-form-item label="菜单名称" :label-width="formLabelWidth">
      <el-input v-model="form2.title" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item v-if="isMenu" label="上级菜单" :label-width="formLabelWidth">
      <el-select v-model="form2.lastMenu" placeholder="请选择">
    <el-option
      v-for="item in options2"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="菜单路由" :label-width="formLabelWidth">
      <el-input v-model="form2.path" placeholder="路由"></el-input>
    </el-form-item>
    <el-form-item label="页面" :label-width="formLabelWidth">
      <el-input v-model="form2.component" placeholder="权限"></el-input>
    </el-form-item>
    <el-form-item label="权限" :label-width="formLabelWidth">
  <el-tag
 :key="tag"
 v-for="tag in form2.auth"
 closable
 :disable-transitions="false"
 @close="handleClose2(tag)">
 {{tag}}
</el-tag>
<el-select  class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm2" @change="handleInputConfirm2">
  <el-option
   v-for="item in options"
   :key="item.value"
   :label="item.label"
   :value="item.value">
  </el-option>
 </el-select>
<el-button v-else class="button-new-tag" size="small" @click="showInput">加入</el-button>
    </el-form-item>
    <el-form-item label="图标" :label-width="formLabelWidth" required>
      <el-popover
  placement="right"
  width="200"
  trigger="click"
  v-model="visible">
  <div>
    <i v-for="(item,i) in gridData" :key="i" :class="item.icon" style="width: 60px;font-size: 25px;text-align: center;" @click="icon(item.icon)"></i>
    <el-link type="primary" @click="fn">更多>>></el-link>
  </div>
  <el-input v-model="form2.titleIcon" placeholder="菜单图标" slot="reference"></el-input>
</el-popover>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="submitForm2(form2)">确 定</el-button>
  </div>
</el-dialog>
      </div>

      <!-- 下面表格 -->
      <div style="width:90%;margin: auto;"></div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        @selection-change="handleSelectionChange">
      <!-- 单选全选 点击按钮 -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="菜单ID" width="120">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column prop="name" label="菜单名称" width="120">
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column prop="address" label="上级菜单" width="120">
          <template slot-scope="scope">{{ scope.row.lastMenu }}</template>
        </el-table-column>
        <el-table-column label="图标" width="120">
      <template slot-scope="scope">
        <i :class="scope.row.titleIcon" style="font-size: 25px;"></i>
      </template>
      </el-table-column>
        <el-table-column prop="address" label="类型" width="120">
          <template slot-scope="scope">{{ scope.row.level }}</template>
        </el-table-column>
        <el-table-column label="权限" width="120">
          <template slot-scope="scope">{{ scope.row.auth }}</template>
        </el-table-column>
        <el-table-column prop="address" label="菜单路由" width="300">
          <template slot-scope="scope">{{ scope.row.path }}</template>
        </el-table-column>
<el-table-column label="操作" width="120">
      <template slot-scope="scope">
        <el-button
          size="50"
          @click="handleEdit(scope.row)">修改</el-button>
      </template>
      </el-table-column>
      </el-table>
      <Pagin :count="total" @pageEvent="fetchData" :pageSize="pageSize"></Pagin>
    </div>
  </div>
</template>

<script>
import {staListAPI,addStaInsertAPI,staUpdateAPI,staDeleteAPI,firstMenu,roleListAPI} from '../../api/me'
export default {
  data() {
    return {
      time:'',
      show:false,
      form: {
        path : '',
        level : '',
        title :'',
        isMenu :'',
        titleIcon :'',
        auth :[],
        component:'',
        lastMenu:''
        },
        form2: {
        path : '',
        level : '',
        title :'',
        isMenu :'',
        titleIcon :'',
        auth :[],
        component:'',
        lastMenu:''
        },
      formLabelWidth:'120px',
      type:'danger',
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dataList:[],
      total: 0, // 列表总行数
      currentPage: 1,
      pageSize: 10,
      tableData: [],
      multipleSelection: [],
      user:'',
      gridData:[{icon:'el-icon-share'},{icon:'el-icon-edit'},{icon:'el-icon-delete'},{icon:'el-icon-search'},
      {icon:'el-icon-setting'},{icon:'el-icon-user'},{icon:'el-icon-bangzhu'},{icon:'el-icon-menu'},{icon:'el-icon-switch-button'},{icon:'el-icon-water-cup'},{icon:'el-icon-mouse'},{icon:'el-icon-service'},{icon:'el-icon-collection'},{icon:'el-icon-money'},{icon:'el-icon-thumb'},{icon:'el-icon-microphone'},{icon:'el-icon-time'},{icon:'el-icon-medal'},{icon:'el-icon-location'} ],
      visible:false,
      options:[],
      options2:[],
      inputVisible: false,
      inputValue: '',
      isMenu:false
    };
  },
  created(){
    this.jia()
    this.roleList()
  },
  components: {
    Pagin: () => import('c/Pagin')
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
   async submitForm(form) {
    const res = await addStaInsertAPI({
      path :'/' + form.path , //主题名
      name :  form.path ,//开始时间
      level : !this.isMenu ,//内容
      title : form.title ,
      isMenu  : true,
      titleIcon : form.titleIcon,
      auth : form.auth,
     component:form.component,
     lastMenu:form.lastMenu
    })

      //请求成功后
      if(res.code == '1'){
        this.jia()
        this.$message.success('添加成功')
       }else{
        this.$message.error('添加失败')
       }
      //关闭
      this.dialogFormVisible = false
    },
    handleEdit(row){
      var arr = []
      if(row.auth.indexOf(',') > '-1'){
        arr = row.auth.split(',')
      }else{
         arr.push(row.auth)
      }
      this.dialogFormVisible2 = true

      this.form2 = row
      this.form2.auth = arr
    },
    icon(con){
   this.form.titleIcon = con
   this.form2.titleIcon = con
   this.visible = false
},
    fn(){
      window.open('https://element.eleme.cn/#/zh-CN/component/icon')
    },
     //修改确定
   async submitForm2(form){
      console.log(form);
      this.dialogFormVisible2 = false
      const res = await staUpdateAPI({
        id : form.id,
        path :'/' + form.path , //主题名
      name :  form.path ,//开始时间
      level : !this.isMenu ,//内容
      title : form.title ,
      isMenu  : true,
      titleIcon : form.titleIcon,
      auth : form.auth,
      component:form.component,
      lastMenu:form.lastMenu
      })
      if(res.code == '1'){
        this.jia()
        this.$message.success('修改成功')
       }else{
        this.$message.error('修改失败')
       }
    },
    //删除
   async clear(){
      var id = []
      this.multipleSelection.forEach(item=>{
        // console.log(item.id,'sda')
         id.push(item.id)
      })
     //请求成功后
     if(id.length > 0){
        const res = await staDeleteAPI(id)
         //请求成功后
         if(res.code == '1'){
        this.jia()
        this.$message.success('删除成功')
       }else{
        this.$message.error('删除失败')
       }
      }
    },
      //新增
      add(){
      this.dialogFormVisible = true
    },
    //模拟假数据
  async  jia(){
    const res = await staListAPI()
      this.dataList = res.data
      this.total = this.dataList.length
      this.fetchData(this.currentPage)
    },
    fetchData (count) {
      var list = this.dataList
      var from = (count - 1) * this.pageSize
      const to = count * this.pageSize
      this.tableData = []
      for (; from < to; from++) {
        if (list[from]) {
          this.tableData.push(list[from])
        }
      }
    },
    //角色
   handleClose(tag) {
    this.form.auth.splice(this.form.auth.indexOf(tag), 1);
   },
   handleClose2(tag) {
    this.form2.auth.splice(this.form.auth.indexOf(tag), 1);
   },
   showInput() {
    this.inputVisible = true;
   },
   handleInputConfirm() {
    let inputValue = this.inputValue;
    if (inputValue) {
     this.form.auth.push(inputValue);
    }
    this.inputVisible = false;
    this.inputValue = '';
   },
   handleInputConfirm2() {
    let inputValue = this.inputValue;
    if (inputValue) {
     this.form2.auth.push(inputValue);
    }
    this.inputVisible = false;
    this.inputValue = '';
   },
   //获取角色列表
   async roleList(){
    const res = await roleListAPI()
    res.data.map(item=>{
       var obj = {
        value : item.role,
        label :item.roleNote
       }
       this.options.push(obj)
    })
   },
   //判断上级菜单
  async rad(){
    const res = await firstMenu()
    res.map(item=>{
       var obj = {
        value : item.title,
        label :item.title,
       }
       this.options2.push(obj)
    })
    this.isMenu = true
   },
   rad2(){
    this.isMenu = false
   }
  },
};
</script>
<style scoped lang="scss">
.flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.input {
  width: 120px;
}
.header {
  width: 20%;
  // border-bottom: 1px solid #e0e0e0;
  padding: 20px 0;
}
.el-tag + .el-tag {
  margin-left: 10px;
 }
 .button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
 }
 .input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
 }
</style>
